import { createBrowserRouter } from "react-router";
import Layout from "@/views/layout";
import Login from "@/views/login";
import NotFound from "@/views/common/404";
import AuthGuard from "./guard";
import type { RouteType } from "./type";

const BASENAME = '/';

// 动态导入模块路由
const modules: Record<string, any> = import.meta.glob('./modules/*.ts', { eager: true })
const moduleRoutes: RouteType[] = []
Object.keys(modules).forEach(key => {
    const module = modules[key]
    moduleRoutes.push(...module.default)
})
const routes: RouteType[] = [
    {
        path: '/',
        Component: AuthGuard,
        children: [
            {
                path: '/login',
                Component: Login,
                meta: {
                    title: '登录',
                }
            },
            {
                path: '/',
                Component: Layout,
                children: [
                    ...moduleRoutes,
                    {
                        path: '*',
                        Component: NotFound,
                        meta: {
                            title: '404',
                        }
                    }
                ]
            },
        ]
    }

]

console.log(routes, 'routes');

export const router = createBrowserRouter(routes, {
    basename: BASENAME
});
